﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台组织机构 -> 部门管理树页面
    /// Author: tim 2010-01-29
    /// </summary>
    public partial class sys_dept_tree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : string.Empty;
                if (string.IsNullOrEmpty(_hidOrganId.Value))
                {
                    Response.Write("<font color='red' family='Arial'>页面初始化错误,缺少必要参数!请重新尝试打开该页面!");
                    Response.End();
                }
                initializePage();
            }
        }

        private void initializePage()
        {
            createTree();
            trvDept.ExpandLevel = 1;
        }

        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            trvDept.Nodes.Add(oRoot);
            createSubNodes(ref oRoot);
        }

        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            string organId = _hidOrganId.Value;
            OrganBLL organBLL = new OrganBLL();
            MsOrgan msOrgan = organBLL.GetOrganInfo(organId);
            if (null == msOrgan) return;

            string organName = msOrgan.organName;
            oRoot.Text = organName;
            oRoot.ImageUrl = "../images/organ/organ.gif"; //需要替换
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
            oRoot.ID = "0_0";

            msOrgan = null;
            organBLL.Release();
            organBLL = null;
        }

        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            string organId = _hidOrganId.Value;
            DeptBLL deptBLL = new DeptBLL();
            List<MsDept> lstDept = deptBLL.GetStairDeptList(organId);            
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            foreach (MsDept eDept in lstDept)
            {
                oTreeNode = createTreeNode(eDept.deptName, eDept.deptId, eDept.isHasChild,organId);
                oRoot.Nodes.Add(oTreeNode);
            }

            lstDept = null;
            deptBLL.Release();
            deptBLL = null;            
        }

        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string deptName, string deptId, 
                                                string hasChild,string organId)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            oNode.Text = deptName;
            string sPageParam = "organId=" + organId + "&deptId=" + deptId;
            string encodeParam = base64Encode(sPageParam);
            oNode.NavigateUrl = "./sys_dept_edit.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/organ/dept.gif";
            oNode.Target = "iFrmEdit";
            oNode.ID = deptId + "_" + "0";
            oNode.Type = "root";

            if (hasChild == "1")
            {
                Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oNode.Nodes.Add(oSubNode);
            }

            return oNode;
        }


        private string base64Encode(string sPageParam)
        {
            string encodeParam = string.Empty;
            if (string.IsNullOrEmpty(sPageParam)) return encodeParam;

            encodeParam = Base64Lib.Encode(sPageParam);
            return encodeParam;
        }


        #region Ajax Methods
        /// <summary>
        /// 获取部门下一级的子部门
        /// </summary>
        /// <param name="deptId">部门Id</param>
        /// <param name="organId">机构Id</param>
        /// <param name="nodeIndex">展开的节点索引</param>
        /// <param name="expandSign">展开标志</param>
        /// <param name="levelStruct">部门层级结构Id串</param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetSubDeptsByDeptId(string deptId, string organId, string nodeIndex,
                            string expandSign, string levelStruct)
        {
            DeptBLL deptBLL = new DeptBLL();
            List<MsDept> lstDept = deptBLL.GetSubDeptList(deptId, organId);
            var newObj = new {
                operType = "DYNLOADNODES",
                nodeIndex = nodeIndex,
                depts = lstDept,
                expandSign = expandSign,
                levelStruct = levelStruct,
                curNodeId = deptId
            };

            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            newObj = null;
            lstDept = null;
            deptBLL.Release();
            deptBLL = null;
            return serExpr;
        }



        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="deptId"></param>
        /// <param name="sNodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteDept(string deptId, string nodeIndex)
        {
            DeptBLL deptBLL = new DeptBLL();
            List<string> lstResult = deptBLL.Delete(deptId);

            var newObj = new { 
                operType = "DEPTDELETE", 
                nodeIndex = nodeIndex, 
                resultSign = lstResult[0], 
                resultMsg = lstResult[1] 
            };

            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            newObj = null;
            lstResult = null;
            deptBLL.Release();
            deptBLL = null;
            return serExpr;
        }

        /// <summary>
        /// get destination parent node struct tree node ids
        /// </summary>
        /// <param name="dtNodeId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetNodeLevelStruct(string dtNodeId)
        {
            DeptBLL deptBLL = new DeptBLL();
            string structIds = deptBLL.GetDeptLevelStruct(dtNodeId);
            var newObj = new {
                operType = "GETSTRUCT",
                resultIds = structIds
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            deptBLL.Release();
            deptBLL = null;
            return serExpr;
        }

        #endregion
    }
}
